package com.atguigu.exam;

import java.util.Arrays;

/**
 * @author xbd
 * @create 2021-05-25 16:47
 */
public class PersonTest {
    public static void main(String[] args) {
        Person[] p = new Person[6];
        p[0] = new Person("xiaomi",25);
        p[1] = new Person("xiaomi",23);
        p[2] = new Person("xiaohua",25);
        p[3] = new Person("huju",30);
        p[4] = new Person("miaoqi",18);
        p[5] = new Person("kali",22);
        Arrays.sort(p);
        System.out.println(Arrays.toString(p));
    }
}

class Person implements Comparable{
    private String name;
    private int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public int compareTo(Object o) {
        if(o instanceof Person){
            Person p = (Person)o;

            if(this.name.equals(p.name)){
                return -Integer.compare(this.age,p.age);
            }else{
                return this.name.compareTo(p.name);
            }
        }
        throw new RuntimeException("类型有误");
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}